<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="关键词" prop="searchWord">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入打样单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dayang:index:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['dayang:index:import']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['dayang:index:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getPage"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column align="center" label="主键" prop="id" v-if="false"/>
      <el-table-column align="center" width="180" label="日期" prop="riqi" />
      <el-table-column align="center" width="180" label="客户名称" prop="khmc" />
      <el-table-column align="center" width="180" label="印品名称" prop="ypmc" />
      <el-table-column align="center" width="180" label="拼版尺寸" prop="cpcc" />
      <el-table-column align="center" width="180" label="色数" prop="yanse" />
      <el-table-column align="center" width="180" label="打样类别" prop="cplb" />
      <el-table-column align="center" width="180" label="个数" prop="num" />
      <el-table-column align="center" width="180" label="单价" prop="danjia" />
      <el-table-column align="center" width="180" label="金额" prop="jine" />
      <el-table-column align="center" width="180" label="部门" prop="bumen" />
      <el-table-column align="center" width="180" label="业务员" prop="ywy" />
      <el-table-column align="center" width="180" label="制单" prop="zdr" />
      <el-table-column align="center" width="180" label="审核" prop="shenhe" />
      <el-table-column align="center" width="180" label="打样单号" prop="dydh" />
      <el-table-column align="center" width="180" label="业务订单号" prop="ywddh" />
      <el-table-column align="center" width="180" label="打样确认" prop="dyqr" />

      <el-table-column align="center" width="100" label="操作" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dayang:index:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dayang:index:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getPage"
    />

    <!-- 添加或修改单表对话框 -->
    <el-dialog :title="model.title" :visible.sync="model.open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="日期">
              <el-date-picker clearable size="small"
                          v-model="form.riqi"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <h1 align="center"><b>打样单</b></h1>
          </el-col>
          <el-col :span="6">
            <el-form-item label="打样单号">
              <el-input v-model="form.dydh" />
            </el-form-item>
          </el-col>
        </el-row>
        <div style="border: solid 1px lightgray;border-radius: 5px;margin-bottom: 12px;padding: 15px 15px 0px">
        <el-row>
          <el-col :span="7">
            <el-form-item label="部门">
              <el-input v-model="form.bumen" />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="业务员">
              <el-input v-model="form.ywy" />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="客户名称">
              <el-input v-model="form.khmc" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="成品尺寸">
              <el-input v-model="form.cpcc" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="材质种类">
              <el-select v-model="form.czzl" clearable>
                <el-option v-for="dict in dictData.czzl" :key="dict" :label="dict" :value="dict" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="纸张要求">
              <el-input v-model="form.zzyq" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="14">
            <el-form-item label="印品名称">
              <el-input v-model="form.ypmc" />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="产品类别">
              <el-select v-model="form.cplb" clearable style="width: 100%;">
                <el-option v-for="dict in dictData.cplb" :key="dict" :label="dict" :value="dict" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="5">
            <el-form-item label="数量">
              <el-input-number v-model="form.num" style="width: 150px"/>
            </el-form-item>
          </el-col>
          <el-col :span="14">
            <el-form-item label="后工">
              <el-select v-model="form1.hougongs" clearable multiple style="width: 100%;">
                <el-option v-for="dict in dictData.hougong" :key="dict" :label="dict" :value="dict" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="切盒">
              <el-select v-model="form.qiehe" clearable>
                <el-option v-for="dict in dictData.qiehe" :key="dict" :label="dict" :value="dict" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="5">
            <el-form-item label="版费">
              <el-input-number v-model="form.banfei" :precision="2" style="width: 150px"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="版金额">
              <el-input-number v-model="form.bje" :precision="2" style="width: 180px"/>
            </el-form-item>
          </el-col>
          <el-col :span="13">
            <el-form-item label="颜色">
              <el-select v-model="form.yanse" clearable>
                <el-option v-for="dict in dictData.yanse" :key="dict" :label="dict" :value="dict" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="17">
            <el-form-item label="板尺寸">
              <el-input v-model="form.bcc" />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="展开尺寸">
              <el-input v-model="form.zkcc" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="19">
            <el-form-item label="备注">
              <el-input v-model="form.beizhu" />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="制单">
              <el-input v-model="form.zdr" />
            </el-form-item>
          </el-col>
        </el-row>
        </div>

        <el-row>
          <el-col :span="5">
            <el-form-item label="单价">
              <el-input-number v-model="form.danjia" :precision="2" style="width: 160px" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="金额">
              <el-input-number v-model="form.jine" :precision="2" style="width: 180px" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="打样时间">
              <el-date-picker clearable size="small"
                          v-model="form.dysj"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择创建时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="打样确认">
              <el-input v-model="form.dyqr" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="16"><el-form-item></el-form-item></el-col>
          <el-col :span="8">
            <el-form-item label="审核">
              <el-input v-model="form.shenhe" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { pageDomain, getDomain, delDomain, saveDomain, genNo } from "@/api/shengchan/dayang";
import { getDictData } from "@/api/shengchan/canshu";
import { getToken } from "@/utils/auth";
import moment from 'moment';

export default {
  name: "Dayang",
  components: {
  },
  data() {
    return {
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      dataList: [],
      // 弹出层标题
      title: "打样费用",
      model: {
        open: false,
        title: ""
      },
      exportUrl: "/dayang/export",
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/dayang/importData"
      },
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        keyword: undefined
      },
      // 表单参数
      form: {},
      form1: {
        hougongs: []
      },
      // 表单校验
      rules: {
        value: [
          { required: true, message: "值不能为空", trigger: "blur" }
        ],
      },
      dictData: {}
    };
  },
  created() {
    this.getPage();
    this.getDict();
  },
  methods: {
    getDict() {
      this.dictData = {}
      getDictData().then(res => {
        if (res.code === 200) {
          this.dictData = res.data
        }
      })
    },
    /** 自定义分页查询 */
    getPage() {
      this.loading = true;
      this.queryParams.params = {};
      pageDomain(this.queryParams).then(res => {
        this.dataList = res.records;
        this.total = res.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.model.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        riqi: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
        dydh: null,
        bumen: null,
        ywy: null,
        khmc: null,
        pbcc: null,
        dylb: null,
        cpcc: '长		宽		高		mm',
        czzl: null,
        zzyq: null,
        ypmc: null,
        cplb: null,
        num: null,
        hougong: null,
        qiehe: null,
        banfei: null,
        bje: null,
        yanse: null,
        bcc: null,
        zkcc: null,
        beizhu: null,
        zdr: this.$store.state.user.nickName,
        danjia: null,
        jine: null,
        dysj: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
        dyqr: null,
        shenhe: null

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getPage();
    },
    /** 搜索按钮操作 */
    handlePage() {
      this.queryParams.current = 1;
      this.getPage();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.loading = true;
      this.reset();
      genNo().then(res => {
        this.loading = false;
        this.form.dydh = res.data;
        this.model.open = true;
        this.model.title = "添加" + this.title;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      if (!id) {
        this.$modal.msgError("请选中一行进行编辑");
        return
      }
      getDomain(id).then(res => {
        this.loading = false;
        this.form = res.data;
        this.form1.hougongs = this.form.hougong ? this.form.hougong.split(',') : [];
        this.model.open = true;
        this.model.title = "修改" + this.title;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          saveDomain(this.form).then(res => {
            this.form.hougong = this.form1.hougongs ? this.form1.hougongs.join(',') : '';
            this.$modal.msgSuccess("保存成功");
            this.model.open = false;
            this.getPage();
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除数据项？').then(() => {
        this.loading = true;
        return delDomain(ids);
      }).then(() => {
        this.loading = false;
        this.getPage();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(this.exportUrl, {
        ...this.queryParams
      }, `${this.title}_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(res, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(res.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getPage();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
